class Solution:
  def isNumber(self, s: str) -> bool:
    safe = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
    valid = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', 'e', '.', '+']
    s = s.strip()
    if len(s) < 1:
      return False
    if s.startswith('+') or s.startswith('-'):
      if len(s) > 1:
        s = s[1:]
      else:
        return False
    if s.startswith('e') or s.endswith('+') or s.endswith('-') or s.endswith('e'):
      return False

    def recurse(s: str, index: int):
      if index == len(s):
        return True
      if s[index] not in valid:
        return False
      if s[index] in safe:
        return recurse(s, index + 1)
      elif s[index] == '.' and len(s) != 1 and '.' not in s[:index] and 'e' not in s[:index]:
        return recurse(s, index + 1)
      elif s[index] == 'e' and 'e' not in s[:index] and (
          s[index - 1] in safe or (s[index - 1] == '.' and index >= 2 and s[index - 2] in safe)):
        return recurse(s, index + 1)
      elif (s[index] == '-' or s[index] == '+') and s[index - 1] == 'e':
        return recurse(s, index + 1)
      else:
        return False

    return recurse(s, 0)


'''
STATUS: SOLVED
TRIES:  12
TIME:   52 MS
MEMORY: 13.8 MB
'''
if __name__ == "__main__":
  solution = Solution()
  print(solution.isNumber('1234'))
